/**
 * NLPK Server end.
 * 
 * Services:
 * 
 * address:     /input
 * use socket.
 * 
 * address:     /
 * main page, list data table.
 */

const WEB_SOCKET_PORT = 6200;
const SERVER_PORT = 6100;

const Koa = require("koa");
const fs = require("fs");
const WebSocket = require("ws");
const path = require('path');

const server = new Koa();
const serve = require('koa-static');

// create the WebSocket connection.
const wsServer = new WebSocket.Server({
    port: WEB_SOCKET_PORT
});

let wsInstance;

wsServer.on("connection", ws => {
    wsInstance = ws;
    ws.on("message", message => {
        console.log("===> ", message);
        ws.send(message);
    });
});

const publicFiles = serve(path.join(__dirname, '../client'), { extensions: ['html']});
publicFiles._name = 'static /client';
server.use(publicFiles);

/**
 * WebSocket automatically sends the message text to main.html
 * /input?message=your text
 */
server.use(async (context, next) => {
    if (context.request.path.startsWith('/input')) {
        const message = context.request.query.message
        wsInstance && wsInstance.send(message);
        
        console.log('Received message from input:', message)

        context.response.status = 200;
    } else {
        await next();
    }    
});

server.use(async (context, next) => {
    context.response.body = "<h1>Page not found!</h1>";
});

server.listen(SERVER_PORT);